// src/plugins/share.ts
// 用户信息

// 获取当前页面路径
const getCurrentPageUrl = () => {
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  return currentPage.route
}

// 获取当前页面的参数
const getCurrentPageOptions = () => {
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  const options = currentPage.options || {}
  // 获取本地存储的参数
  try {
    const storageOptions = uni.getStorageSync('member')
    // console.log("🚀 ~ getCurrentPageOptions ~ storageOptions:", JSON.parse(storageOptions).profile)
    // 检查是否包含 member_id，如果没有则添加 member_id=1
    if (!options.member_id) {
      options.member_id = JSON.parse(storageOptions).profile.id
    }
  } catch (e) {}
  // console.log("🚀 ~ getCurrentPageOptions ~ options:", options)

  return options
}

const share = {
  // 分享到好友
  onShareAppMessage() {
    let currentPageUrl = getCurrentPageUrl()
    console.log('🚀 ~ onShareAppMessage ~ currentPageUrl:', currentPageUrl)

    const options = getCurrentPageOptions()
    const query = Object.entries(options)
      .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
      .join('&')
    console.log('Current Page URL:', currentPageUrl) // 调试信息
    if (currentPageUrl == 'giftPack/payment') {
      currentPageUrl = 'giftPack/purch/purch'
    }

    console.log('Query:', query) // 调试信息
    return {
      title: '悦合集悦惠通', // 分享标题
      path: `/pages/index/index`, // 分享路径
      imageUrl: 'https://image.yueheji.com/YueHeJiApp/image20231211/logo.jpeg', // 分享图片
    }
  },

  // 分享到朋友圈
  onShareTimeline() {
    const currentPageUrl = getCurrentPageUrl()
    const options = getCurrentPageOptions()
    const query = Object.entries(options)
      .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
      .join('&')
    return {
      title: '悦合集悦惠通', // 分享标题
      query: `/pages/index/index`, // 分享路径参数
      imageUrl: 'https://image.yueheji.com/YueHeJiApp/image20231211/logo.jpeg', // 分享图片
    }
  },
}

export default share
